class_name PlayerStateChecker
extends Node

@export var player: Player

@onready var stand_collision: CollisionShape2D = %StandCollision

@onready var crouch_collision: CollisionShape2D = %CrouchCollision

@onready var stand_wall_detector: RayCast2D = %StandWallDetector

@onready var stand_wall_top_detector: RayCast2D = %StandWallTopDetector

@onready var crouch_head_front_collide_detector: RayCast2D = %CrouchHeadFrontCollideDetector

@onready var crouch_head_back_collide_detector: RayCast2D = %CrouchHeadBackCollideDetector

#region States
var is_floor_detected: bool = false

var is_wall_detected: bool = false

var is_wall_top_detected: bool = false

var is_head_collide_detected: bool = false
#endregion


func on_update(_delta: float) -> void:
	is_floor_detected = player.is_on_floor()
	is_wall_detected = stand_wall_detector.is_colliding()
	is_wall_top_detected = is_wall_detected and not stand_wall_top_detector.is_colliding()
	is_head_collide_detected = crouch_head_front_collide_detector.is_colliding() or crouch_head_back_collide_detector.is_colliding()

	if player.player_tag_manager.has_tag(player.player_tag_manager.State_Ground_Crouch):
		change_to_crouch_collision()
	else:
		change_to_stand_collision()


func change_to_stand_collision() -> void:
	stand_collision.disabled = false
	stand_wall_detector.enabled = true
	stand_wall_top_detector.enabled = true
	
	crouch_collision.disabled = true
	crouch_head_front_collide_detector.enabled = false
	crouch_head_back_collide_detector.enabled = false


func change_to_crouch_collision() -> void:
	stand_collision.disabled = true
	stand_wall_detector.enabled = false
	stand_wall_top_detector.enabled = false

	crouch_collision.disabled = false
	crouch_head_front_collide_detector.enabled = true
	crouch_head_back_collide_detector.enabled = true
